﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Web.UI;
using System.Reflection;

namespace Dragon.Prototype1.Infrastructure.Controls
{
    public abstract class jQueryControl : WebControl
    {
        const string scriptDeclarationStart = "<script type=\"text/javascript\">";
        const string scriptDeclarationEnd = "</script>";
        const string jQueryDocumentReadyStart = "$(document).ready(function(){";
        const string jQueryDocumentReadyEnd = "});";

        public jQueryControl()
        {
            
        }

        private Dictionary<string, object> _jQueryParameters = new Dictionary<string, object>();
        public Dictionary<string, object> jQueryParameters
        {
            get { return _jQueryParameters; }
        }

        public string Selector
        {
            get;
            set;
        }

        public virtual void RenderJavaScriptStart(HtmlTextWriter writer)
        {
            writer.WriteLine(scriptDeclarationStart);
            writer.WriteLine(jQueryDocumentReadyStart);            
        }

        public virtual void RenderJavaScriptEnd(HtmlTextWriter writer)
        {
            writer.WriteLine(jQueryDocumentReadyEnd);
            writer.WriteLine(scriptDeclarationEnd);            
        }

        protected string GeneratejQueryParameterArray()
        {
            if (_jQueryParameters.Count == 0)
            {
                return string.Empty;
            }
            StringBuilder sb = new StringBuilder();
            sb.Append("{");
            foreach (KeyValuePair<string, object> param in _jQueryParameters)
            {
                sb.AppendFormat(string.Format("{0}:{1},", param.Key, GetJavaScriptValueString(param.Value)));
            }
            sb.Remove(sb.Length - 1, 1);
            sb.Append("}");
            return sb.ToString();
        }

        protected string GetJavaScriptValueString(object value)
        {
            if(value.GetType()==typeof(string))
            {
                return string.Format("'{0}'",value);
            }
            else if (value.GetType() == typeof(Boolean))
            {
                return value.ToString().ToLower();
            }
            else if (value.GetType().IsEnum)
            {
                return string.Format("'{0}'", Enum.Format(value.GetType(), value, "G")); 
            }
            else
            {
                return value.ToString();
            }
        }

        protected virtual void AddjQueryParametersToRender()
        {
            PropertyInfo[] properties= this.GetType().GetProperties();

            foreach(PropertyInfo property in properties)
            {
               object[] attrs= property.GetCustomAttributes(typeof(jQueryParameterAttribute), true);
                if(attrs.Length>0)
                {
                    object propertyGetValue = property.GetValue(this, null);
                    jQueryParameterAttribute attr=(jQueryParameterAttribute)attrs[0];
                    object defaultValue = attr.DefaultValue;
                    if (propertyGetValue == null || propertyGetValue.Equals(defaultValue))
                    {
                        continue;
                    }
                    this.jQueryParameters.Add(attr.ParameterName, propertyGetValue);
                }
            }
        }

        public virtual void RenderJavaScriptContents(HtmlTextWriter writer)
        {
            this.AddjQueryParametersToRender();
        }
    }
}
